#ifndef __IPCAM_H__
#define __IPCAM_H__

#include <jni.h>
//#include <android/log.h>
#include <semaphore.h>
#include <stdio.h>

#include "tcpclient.h"

#define MAX_RGB16_DATA_NUM		4

typedef struct _stRGB16Data
{
	unsigned char	byIsValid;
	unsigned char	szReserved[3];
	unsigned short	nWidth;
	unsigned short	nHeight;
	unsigned char	*pszRGBData;
}RGB16_DATA;

//#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-activity", __VA_ARGS__))
//#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "native-activity", __VA_ARGS__))

class CIpCam
{
public:
	CIpCam();
	virtual ~CIpCam();
	
public:
	void AutoLogin();
	void StopService();
	
public:
	BOOL OnLogin(const char *pszIpAddr, int iPort1, int iPort2, const char *pszUserName, const char *pszPassword);
	BOOL OnLogout();
	void HeartBeat();
	BOOL RealPlay(BOOL bIsMainStream);
	BOOL StopPlay();
	BOOL OpenSound();
	BOOL CloseSound();
	BOOL PTZControl(int nCommand, int nSpeed);
	BOOL StartRecord(const char *pszFileName);
	BOOL StopRecord();
	
public:
	void StreamNotify(int index);
	
public:
	void LoginResponse(unsigned short nSessionID, unsigned short nMessageLen, const char *pszMessage);
	void StreamResponse(unsigned short nSessionID, unsigned short nMessageLen, const char *pszMessage);
	void PTZCtrlResponse(unsigned short nSessionID, unsigned short nMessageLen, const char *pszMessage);
	void ProcVideodata(const char *pszMessage, unsigned short nMessageLen);
	void ProcAudiodata(const char *pszMessage, unsigned short nMessageLen);
	
public:
	void AttachJavaCallback(BOOL bIsCmdFunc);
	void DetachJavaCallback(BOOL bIsCmdFunc);
	
public:
	void IRLAB_ProcMessage(
										int					iConnType,
										int					iServID,
										unsigned char		byEncryption,
										unsigned short		nCommand,
										unsigned int		dwResult,
										unsigned short		nSessionID,
										unsigned short		nMessageLen,
										const char			*pszMessage);
	
public:
	static void IRLAB_ClientRecvData(
										int					iConnType,
										int					iServID,
										unsigned char		byEncryption,
										unsigned short		nCommand,
										unsigned int		dwResult,
										unsigned short		nSessionID,
										unsigned short		nMessageLen,
										const char			*pszMessage,
										void				*pContext);
	
public:
	const char *GetIpAddr();
	void SetIpAddr(const char *pszIpAddr);
	
	int GetCmdPort();
	void SetCmdPort(int iPort);
	
	int GetMediaPort();
	void SetMediaPort(int iPort);
	
	const char *GetUserName();
	void SetUserName(const char *pszUserName);
	
	const char *GetPassword();
	void SetPassword(const char *pszPassword);
	
public:
	BOOL InitDecoder();
	BOOL UninitDecoder();
	BOOL DecoderNal(unsigned char *pszVideoData, unsigned int dwFrameLength);

public:
	BOOL StartThread();
	void StopThread();
	
	BOOL StartDisplayThread();
	void StopDisplayThread();
	
public:
	void CheckStatus();
	void ProcDisplay();
	
public:
	static void *CheckStatusThread(void *pParam);
	static void *DisplayThread(void *pParam);
	
private:
	sem_t			m_semaphore;
	BOOL			m_bIsLogined;
	BOOL			m_bIsProcessing;
	unsigned short	m_nSessionID;
	int				m_iHBCount;
	int				m_iWaitTime;
	BOOL			m_bIsVideoOpened;
	BOOL			m_bIsAudioOpened;
	BOOL			m_bIsMainStream;
	unsigned short	m_nResult;
	BOOL			m_bIsAutoLogin;
	BOOL			m_bIsStopService;
	
private:
	unsigned char	*m_szVideoData;
	unsigned char	*m_szAudioData;
	unsigned int	m_dwVideoFrameLen;
	unsigned int	m_dwAudioFrameLen;
	BOOL			m_bIsFirst;
	RGB16_DATA		m_stData[MAX_RGB16_DATA_NUM];
	unsigned char	m_byIndex;
	unsigned char	m_byDispIndex;
	FILE						*m_pFile;
	BOOL						m_bIsRecording;
	BOOL						m_bIsStatusThreadEnd;
	BOOL						m_bIsDisplayThreadEnd;
	
private:
	pthread_t		m_hThread;
	BOOL			m_bIsStop;
	
	pthread_t		m_hDispThread;
	BOOL			m_bIsDispStop;
	sem_t			m_semDisplay;
	
private:
	CTcpClient		m_oCmdClient;
	CTcpClient		m_oMdaClient;
	BOOL			m_bIsDisplaying;
	
private:
	char			m_szIpAddr[100];
	int				m_iCmdPort;
	int				m_iMdaPort;
	char			m_szUserName[32];
	char			m_szPassword[32];
	
public:
	JavaVM 			*m_poCmdJavaVM;
	jobject 		m_objCmdInterface;
    JNIEnv			*m_pCmdEnv;
    BOOL			m_bIsCmdAttached;
    jmethodID		m_jmidProcess;
    jobject 		m_objCmdCallBack;
    
    JavaVM 			*m_poMdaJavaVM;
	jobject 		m_objMdaInterface;
    JNIEnv			*m_pMdaEnv;
    BOOL			m_bIsMdaAttached;
    jmethodID		m_jmidProcVideoData;
    jmethodID		m_jmidProcAudioData;
    jobject 		m_objMdaCallBack;
};

#endif //__IPCAM_H__
